﻿using Microsoft.LightSwitch;
using System;

namespace LightSwitchApplication
{
	public partial class Language
	{
		partial void Name_Validate(EntityValidationResultsBuilder results)
		{
			try
			{
				var culture = this.CreateCulture(this.Name);

				if (!string.Equals(this.Name, culture.Name, StringComparison.Ordinal))
				{
					results.AddPropertyError(string.Format("Inappropriate culture name '{0}'. Exists culture with name '{1}'.", this.Name, culture.Name));
				}
			}
			catch (System.Globalization.CultureNotFoundException ex)
			{
				results.AddPropertyError(ex.Message);
			}
		}

		partial void EnglishName_Compute(ref string result)
		{
			var culture = this.CreateCulture(this.Name);

			result = culture.EnglishName;
		}

		partial void NativeName_Compute(ref string result)
		{
			var culture = this.CreateCulture(this.Name);

			result = culture.NativeName;
		}

		private System.Globalization.CultureInfo CreateCulture(string cultureName)
		{
			if (string.IsNullOrEmpty(cultureName))
			{
				return System.Globalization.CultureInfo.InvariantCulture;
			}
			else
			{
				return new System.Globalization.CultureInfo(cultureName);
			}
		}
	}
}